var LBS = {};
(function()
	{
		LBS.curPosition=null;
		LBS.getPositionTimer=null;
		LBS.mapObject=null;
		LBS.city=null;
		LBS.transit=null;
		LBS.Route=function(map,currentPoint)
		{
			this.map=map;
			this.currentPoint=currentPoint;
			this.busRouteReady=false;
			this.walkingRouteReady=false;
			this.routeCallbackCount=null;
			this.busRouteResults=null;
			this.destPoint=null;
			this.walkingRouteResults=null;
			this.drivingRouteResults=null;
			this.drivingRouteReady=null;
			this.findRouteManager=function(address)
			{
				var th=this;
				th.routeCallbackCount=0;
				th.timer=window.setInterval(
					function()
					{
						th.routeCallbackCount+=1;
						th.findBusRoute(address);
						th.findWalkingRoute(address);
						th.findDrivringRoute(address);
						if(th.routeCallbackCount>10 && th.timer)
						{
							window.clearInterval(th.timer);
							th.timer=null;
						}
						if(!th.currentPoint)
						{
							return;
						}
						if(!LBS.city)
						{
							return;
						}
						if(th.busRouteReady && th.walkingRouteReady && th.drivingRouteReady)
						{
							window.clearInterval(th.timer);
							th.timer=null;
							if(th.busRouteResults && th.busRouteResults.getNumPlans()>0)
							{
								
								var innerHTML="";
								for(i=0;i<th.busRouteResults.getNumPlans();i++)
								{	var plan = th.busRouteResults.getPlan(i);
									innerHTML+="<li>";
									innerHTML+="Time: "+plan.getDuration(true)+"</br>";
									innerHTML+="Bus distance: "+plan.getDistance(true);
									innerHTML+="</br>";
									innerHTML+=plan.getDescription();
									innerHTML+="</li>";
								}
								document.getElementById("busListView").innerHTML=innerHTML;
								$('#busListView').listview('refresh');
    								commonOut(plan.getDistance(false));
    							}	
							if(th.walkingRouteResults && th.walkingRouteResults.getNumPlans()>0)
							{
								var innerHTML="";
								for(i=0;i<th.walkingRouteResults.getNumPlans();i++)
								{	
									var plan = th.walkingRouteResults.getPlan(i);
									innerHTML+="<li>";
									innerHTML+="Time: "+plan.getDuration(true)+"</br>";
									innerHTML+="Walking distance: "+plan.getDistance(true)+"</li>";
								}
								document.getElementById("walkingListView").innerHTML=innerHTML;
								$('#walkingListView').listview('refresh');
    								commonOut("walking distance: "+plan.getDistance(false) );
							}
							if(th.drivingRouteResults && th.drivingRouteResults.getNumPlans()>0)
							{
								
								var innerHTML="";
								for(i=0;i<th.drivingRouteResults.getNumPlans();i++)
								{	
									var plan = th.drivingRouteResults.getPlan(i);
									innerHTML+="<li>";
									innerHTML+="Time: "+plan.getDuration(true)+"</br>";
									innerHTML+="Driving distance: "+th.drivingRouteResults.getPlan(i).getDistance(true)+"</li>";
								}
								document.getElementById("drivingListView").innerHTML=innerHTML;
								$('#drivingListView').listview('refresh');
    								commonOut("driving distance: "+plan.getDistance(false) );

							}

						}
					}
					, 1000);

			}
			this.findRoute=function(address)
			{
				var th=this;
				if(!th.currentPoint)
				{
					var geolocation = new BMap.Geolocation();//get current point
					geolocation.getCurrentPosition(function(r) {


						if (this.getStatus() == BMAP_STATUS_SUCCESS) 
						{
							th.currentPoint=r.point;
							//get city from point
							var gc = new BMap.Geocoder();    
							gc.getLocation(th.currentPoint, function(rs){
							        var addComp = rs.addressComponents;
							        LBS.city=addComp.city;
							        commonOut("current city:"+addComp.city);
							    });        
						} else {
							//alert('failed' + this.getStatus());
						}
					},
					{
						enableHighAccuracy: true
					});
				}
				var myGeo = new BMap.Geocoder();
				// 将地址解析结果显示在地图上,并调整地图视野
				myGeo.getPoint(address,
				function(point) {
					if (point)
					 {
					 	//LBS.curLocation=point;
					 	//commonOut(point.lng);
					 	//commonOut(point.lat);
					 	th.destPoint=point;
					}
					 else {
						//alert('cannot find this location');
					}},
				LBS.city);
				this.findRouteManager(address);
			}
			this.findBusRoute=function(address)
			{
				var th=this;
			 	if(th.currentPoint && th.destPoint)
			 	{
			 		var map=LBS.mapObject;
					var transit = new BMap.TransitRoute(th.currentPoint, { renderOptions: {map: th.map} ,onSearchComplete:function (results)
					{
							if (transit.getStatus() != BMAP_STATUS_SUCCESS)
							{
								
    							}
    							
    							th.busRouteResults=results;
							th.busRouteReady=true;
						}

					}	);
					transit.search(th.currentPoint, th.destPoint);
			 	}
					 	
			}
			this.findWalkingRoute=function(address)
			{
				var map=LBS.mapObject;
				var th=this;
				if(th.currentPoint && th.destPoint)
				{
					var walking = new BMap.WalkingRoute(th.currentPoint, {renderOptions:{map: map}, autoViewport: true,
						onSearchComplete:function(results)
						{
							if (walking.getStatus() != BMAP_STATUS_SUCCESS)
							{
								
							}
							th.walkingRouteResults=results;
							th.walkingRouteReady=true;
						}
					});
					walking.search(th.currentPoint, th.destPoint);
				}
			}
			this.findDrivringRoute=function()
			{
				var map=LBS.mapObject;
				var th=this;
				if(th.currentPoint && th.destPoint)
				{
					var driving = new BMap.DrivingRoute(th.currentPoint, {renderOptions:{map: map}, autoViewport: true,
						onSearchComplete:function(results)
						{
							if (driving.getStatus() != BMAP_STATUS_SUCCESS)
							{
								
							}
							th.drivingRouteResults=results;
							th.drivingRouteReady=true;
						}
					});
					driving.search(th.currentPoint, th.destPoint);
				}
			}
		}
		
		LBS.getDistance=function(startPoint,endPoint)
		{
			var map=LBS.mapObject;
			return map.getDistance(startPoint,endPoint);
		}
		LBS.findLocation=function(address,city)
		{
				// 创建地址解析器实例
			var myGeo = new BMap.Geocoder();
			// 将地址解析结果显示在地图上,并调整地图视野
			myGeo.getPoint(address,
			function(point) {
				if (point)
				 {
				 	//LBS.curLocation=point;
				 	commonOut(point.lng);
				 	commonOut(point.lat);
				 	if(LBS.curPosition)
				 	{
				 		var distance=LBS.getDistance(LBS.curPosition,point);	
				 		commonOut('distance: '+distance);
				 		LBS.refreshMap(point);
				 	}
				 	
				}
				 else {
					alert('cannot find this location');
				}},
			city);
		}
		LBS.getCurrentPosition=function()
		{
			if(LBS.getPositionTimer)
			{
					return;
			}
			var geolocation = new BMap.Geolocation();
			geolocation.getCurrentPosition(function(r) {


				if (this.getStatus() == BMAP_STATUS_SUCCESS) {
					LBS.curPosition=r.point;
					//get city from point
					var gc = new BMap.Geocoder();    
					gc.getLocation(r.point, function(rs){
					        var addComp = rs.addressComponents;
					        LBS.city=addComp.city;
					        commonOut("current city:"+addComp.city);
					    });        
				} else {
					alert('failed. return code is ' + this.getStatus());
					retcode=this.getStatus();
					if(retcode==2)
					{
						commonOut('位置结果未知。');
					}
					if(retcode==3)
					{
						commonOut('导航结果未知。');
					}
					if(retcode==4)
					{
						commonOut('非法密钥');
					}
					if(retcode==5)
					{
						commonOut('非法请求');
					}
					if(retcode==6)
					{
						commonOut('没有权限');
					}
					if(retcode==7)
					{
						commonOut('服务不可用');
					}
					if(retcode==8)
					{
						commonOut('超时。');
					}
				}
			},
			{
				enableHighAccuracy: true
			});
			LBS.getPositionTimer=window.setInterval("LBS.postProcAfterGetPosition();",5000);
		}
		LBS.refreshMap=function(point)// id is the id of containner
		{
			// 百度地图API功能
			if(!LBS.mapObject)
			{
				return;
			}
			var map = LBS.mapObject;
			map.enableScrollWheelZoom();
			var point = new BMap.Point(point.lng,point.lat);
			map.centerAndZoom(point, 12);
			var mk = new BMap.Marker(point);
        			map.addOverlay(mk);
        			map.panTo(point);
		}
		LBS.postProcAfterGetPosition=function()//a callback function. Used by SetInterval to check if currenti position is got.
		{
			if(LBS.curPosition)
			{
				var point =LBS.curPosition;
				LBS.refreshMap(point);
				
			}
			else
			{
				commonOut('did not get current positon within allowed time.');
			}
			window.clearInterval(LBS.getPositionTimer);
			LBS.getPositionTimer=null;
		}
		LBS.createMap=function(id)
		{
			LBS.mapObject=new BMap.Map(id);
		}
		LBS.start=function() 
		{
			if(!LBS.mapObject)
			{
				commonOut('map objecit is null. use createMap first');
				return;
			}
			LBS.getCurrentPosition();
			
		}
	}
	)();
		
		

	